//
//  DataBaseManager.m
//  UI14_DataBase
//
//  Created by LIMING on 14-10-2.
//  Copyright (c) 2014年 Lee All rights reserved.
//

#import "DataBaseManager.h"
static sqlite3 *dbPoint = nil;

@implementation DataBaseManager
+ (void)open
{
    if (dbPoint) {
        return;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"FreeTime.rdb"];
    sqlite3_open(path.UTF8String, &dbPoint);
}
+ (void)createWeiBoUserInfo
{
    [self open];
    NSString *sql = @"create table tb_weibo(username varchar(100), uid varchar(20), token varchar(100), url varchar(200))";
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
#pragma 创建用户表
+ (void)create
{
    [self open];
    NSString *sql = @"create table tb_user(username varchar(100),gender varchar(20),age varchar(20),height varchar(20), weight varchar(20))";
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
#pragma 创建数据表
+ (void)createDataTable
{
    [self open];
    NSString *sql = @"create table tb_data(username varchar(100),spoValue varchar(20),bpmValue varchar(20),piValue varchar(20),timeString varchar(50))";
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (void)close
{
    sqlite3_close(dbPoint);
}
+ (void)insert:(UserInfoModel *)model
{
    [self open];
    NSString *sql = [NSString stringWithFormat:@"insert into tb_user values('%@','%@','%@','%@','%@')", model.username, model.gender, model.age, model.height, model.weight];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (void)insertWeiBoInfo:(WeiboInfoModel *)model
{
    [self open];
    NSString *sql = [NSString stringWithFormat:@"insert into tb_weibo values('%@','%@','%@','%@')", model.username, model.uid,model.token,model.url];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (void)remove:(UserInfoModel *)model
{
    [self open];
    NSString *sql = [NSString stringWithFormat:@"delete from tb_user where username = '%@'", model.username];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
#pragma 插入测量的数据
+ (void)insertData:(PersonalDataModel *)model
{
    [self open];
    NSString *sql = [NSString stringWithFormat:@"insert into tb_data values('%@', '%@', '%@', '%@', '%@')", model.username, model.spoValue,model.bpmValue,model.piValue,model.timeString];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
#pragma 删除测量的数据
+ (void)removeData:(PersonalDataModel *)model
{
    [self open];
    NSString *sql = [NSString stringWithFormat:@"delete from tb_data where username = '%@'", model.username];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (NSArray *)select
{
    [self open];
    //创建数据库替身临时保存数据
    sqlite3_stmt *stmt = nil;
    //创建sql语句
    NSString * sql = @"select * from tb_user";
    //执行语句
    int result = sqlite3_prepare_v2(dbPoint, sql.UTF8String, -1, &stmt, NULL);
    //判断是否成功
    if (result == SQLITE_OK) {
        //创建数组来保存查询的数据
        NSMutableArray *array = [NSMutableArray array];
        //如果还有下一行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //获取数据
            const unsigned char *username = sqlite3_column_text(stmt, 0);
            const unsigned char *gender = sqlite3_column_text(stmt, 1);
            const unsigned char *age = sqlite3_column_text(stmt, 2);
            const unsigned char *height = sqlite3_column_text(stmt, 3);
            const unsigned char *weight = sqlite3_column_text(stmt, 4);
            //封装成model
            UserInfoModel * model = [[UserInfoModel alloc]init];
            model.username = [NSString stringWithUTF8String:(const char *)username];
            model.gender = [NSString stringWithUTF8String:(const char *)gender];
            model.age = [NSString stringWithUTF8String:(const char *)age];
            model.height = [NSString stringWithUTF8String:(const char *)height];
            model.weight = [NSString stringWithUTF8String:(const char *)weight];
            [array addObject:model];
        }
        //删掉替身
        sqlite3_finalize(stmt);
        return array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}
+ (WeiboInfoModel *)selectWeiBoUserInfo
{
    [self open];
    //创建数据库替身临时保存数据
    sqlite3_stmt *stmt = nil;
    //创建sql语句
    NSString * sql = @"select * from tb_weibo";
    //执行语句
    int result = sqlite3_prepare_v2(dbPoint, sql.UTF8String, -1, &stmt, NULL);
    //判断是否成功
    if (result == SQLITE_OK) {
         WeiboInfoModel * model = [[WeiboInfoModel alloc]init];
        //如果还有下一行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //获取数据
            const unsigned char *username = sqlite3_column_text(stmt, 0);
            const unsigned char *uid = sqlite3_column_text(stmt, 1);
            const unsigned char *token = sqlite3_column_text(stmt, 2);
            const unsigned char *url = sqlite3_column_text(stmt, 3);
            //封装成model
           
            model.username = [NSString stringWithUTF8String:(const char *)username];
            model.uid = [NSString stringWithUTF8String:(const char *)uid];
            model.token = [NSString stringWithUTF8String:(const char *)token];
            model.url = [NSString stringWithUTF8String:(const char *)url];
        }
        //删掉替身
        sqlite3_finalize(stmt);
        return model;
    }
    sqlite3_finalize(stmt);
    return nil;
}

@end
